package cs342.uwosh.edu.MyWorldApp;
import cs342.uwosh.edu.R;
import cs342.uwosh.edu.MyWorldApp.cloud.CloudLinker;
import cs342.uwosh.edu.MyWorldApp.misc.AsyncMethod;
import cs342.uwosh.edu.MyWorldApp.misc.BackgroundMethod;
import cs342.uwosh.edu.MyWorldApp.storage.Location;
import cs342.uwosh.edu.MyWorldApp.storage.User;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;

public class LogInActivity extends Activity {

	EditText unBox;
	EditText pwBox;

	/**
	 * Initializes references for user name and password text boxes for later
	 * use
	 */

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		this.getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
		unBox = ((EditText) findViewById(R.id.usernameTextBox));
		pwBox = ((EditText) findViewById(R.id.passwordTextBox));
	}

	/**
	 * Attempts to log the user in
	 */

	public void okButton(View v) {
		String username = unBox.getText().toString();
		String password = pwBox.getText().toString();
		if (isValidLogInData(username, password)) {
			CloudLinker l = new CloudLinker(getBaseContext());
			AsyncMethod<CloudLinker> am2 = new AsyncMethod<CloudLinker>(CloudLinker.class, l, "initializeUser");
			am2.execute(username);
			User user = (User)am2.getResult();
			
			AsyncMethod<CloudLinker> am3 = new AsyncMethod<CloudLinker>(CloudLinker.class, l, "UpdateFriendsLocations");
			am3.execute(user);
			
			//TODO -- CREATE NEW USER
			// ADD LOG IN STEALTH MODE OPTION
			// ACCESS CLOUD AND RETRIEVE USER INFORMATION
			// 	-USER NAME
			//	-LOCATION
			//  -FRIENDSLIST
			// if(!stealthed)
			//	-FRIENDSLOCATIONS
			
			BackgroundMethod<CloudLinker> bm = new BackgroundMethod<CloudLinker>(CloudLinker.class, l, "UpdateFriendsLocations", 1000*60 /* 60 seconds */);
			//bm.doInBackground();
			//	This update should trigger the redisplaying of the current activity
			
			
			Intent myIntent = new Intent(LogInActivity.this,
					MyWorldAppActivity.class);
			myIntent.putExtra("user", user);
			startActivity(myIntent);
			finish(); // no need to keep on stack
		} else {
			Toast.makeText(getBaseContext(), "Try logging in again",
					Toast.LENGTH_LONG).show();
			Toast.makeText(getBaseContext(), "Or create new account",
					Toast.LENGTH_LONG).show();
			openOptionsMenu();
		}
		

		//TODO -- CREATE NEW USER
		// ADD LOG IN STEALTH MODE OPTION
		// ACCESS CLOUD AND RETRIEVE USER INFORMATION
		// 	-USER NAME
		//	-LOCATION
		//  -FRIENDSLIST
		// if(!stealthed)
		//	-FRIENDSLOCATIONS
		
	}

	/**
	 * Quits QuizApp
	 */

	public void cancelButton(View v) {
		onBackPressed();
	}

	/**
	 * Checks the users login data
	 */

	public boolean isValidLogInData(String username, String password) {
		boolean isValid = false;
		CloudLinker l = new CloudLinker(this);
		AsyncMethod<CloudLinker> am = new AsyncMethod<CloudLinker>(CloudLinker.class, l, "checkUser");
		am.execute(username, password);
		String response = (String)am.getResult();
		Toast.makeText(getBaseContext(), response,
				Toast.LENGTH_LONG).show();
		if (response.equals("Login Validated")) {
			isValid = true;
			
		}
		return isValid;
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.login_menu, menu);
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.cnaOptionsMenu:
			createNewAccount();
			break;
		}
		return true;
	}

	public void createNewAccount() {
		Intent myIntent = new Intent(LogInActivity.this,
				CreateNewAccountActivity.class);
		startActivity(myIntent);
	}

}
